package bb.lanxing.fragment.sport;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.github.mikephil.charting.utils.Utils;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

import bb.lanxing.R;
import bb.lanxing.adapter.sport.SportRecordDataAdapter;
import bb.lanxing.calc.PaceHelper;
import bb.lanxing.fragment.base.BaseFragment;
import bb.lanxing.model.database.Workout;
import bb.lanxing.model.json.WorkoutExtraInfo;
import bb.lanxing.util.CommonUtil;
import bb.lanxing.util.DateUtil;
import gov.nist.core.Separators;

public class SportRecordDataFragment extends BaseFragment {
    private Workout workout;

    private final DecimalFormat decimalFormat = new DecimalFormat("0.00");
    private final DecimalFormat decimalFormat2 = new DecimalFormat("0.0");
    private final DecimalFormat decimalFormat4 = new DecimalFormat("0.000");

    public SportRecordDataFragment(Workout workout) {
        this.workout = workout;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_sport_record_data, container, false);
        TextView distance = view.findViewById(R.id.distance);
        distance.setText(CommonUtil.getFormatDistance(workout.getDistance()));

        setTimeList(view);
        setSpeedList(view);
        setAltitudeList(view);
        setPowerList(view);
        setHeartRateList(view);
        setCadenceList(view);
        setGeneralList(view);
        return view;
    }

    private void setTimeList(View view) {
        RecyclerView timeList = view.findViewById(R.id.timeList);
        timeList.setLayoutManager(new GridLayoutManager(getContext(), 3));
        List<String> valueList = new ArrayList<>();
        List<String> nameList = new ArrayList<>();
        valueList.add(DateUtil.format(workout.getDuration() * 1000, 2));
        nameList.add(getString(R.string.workout_detail_label_sport_duration));
        valueList.add(DateUtil.format(workout.getEndTime() - workout.getStartTime(), 2));
        nameList.add(getString(R.string.workout_detail_label_total_duration));
        valueList.add(DateUtil.format(workout.getEndTime() - workout.getStartTime() - workout.getDuration() * 1000, 2));
        nameList.add(getString(R.string.sport_tag_pause_time));
        SportRecordDataAdapter sportRecordDataAdapter = new SportRecordDataAdapter(valueList, nameList);
        timeList.setAdapter(sportRecordDataAdapter);
        TextView startTime = view.findViewById(R.id.startTime);
        startTime.setText(DateUtil.format(workout.getStartTime(), 8));
        TextView endTime = view.findViewById(R.id.endTime);
        endTime.setText(DateUtil.format(workout.getEndTime(), 8));
    }

    private void setSpeedList(View view) {
        RecyclerView speedList = view.findViewById(R.id.speedList);
        speedList.setLayoutManager(new GridLayoutManager(getContext(), 3));
        List<String> valueList = new ArrayList<>();
        List<String> nameList = new ArrayList<>();
        if (workout.getSport() == Workout.TYPE_TRAINING) {
            valueList.add(decimalFormat2.format(workout.getAvgSpeed() * 3.6) + "km/h");
            valueList.add(decimalFormat2.format((workout.getDistance() / workout.getDuration()) * 3.6) + "km/h");
        } else {
            valueList.add(decimalFormat2.format((workout.getDistance() / workout.getDuration()) * 3.6) + "km/h");
            valueList.add(decimalFormat2.format((workout.getDistance() / (workout.getEndTime() - workout.getStartTime())) * 1000 * 3.6) + "km/h");
        }
        nameList.add(getString(R.string.workout_detail_label_sport_avg_speed));
        nameList.add(getString(R.string.workout_detail_label_total_avg_speed));
        valueList.add(decimalFormat.format(workout.getMaxSpeed() * 3.6) + "km/h");
        nameList.add(getString(R.string.workout_detail_label_max_speed));
        // 这个判断存疑
//        if (workout.getSport() == Workout.TYPE_RUNNING || workout.getSport() == Workout.TYPE_WALKING) {
        PaceHelper paceHelper = new PaceHelper();
        paceHelper.init(workout);
        if (paceHelper.isPaceValid()) {
            valueList.add(paceHelper.getAvgPace());
            nameList.add(getString(R.string.workout_detail_label_avg_pace));
            valueList.add(paceHelper.getMinPace());
            nameList.add(getString(R.string.workout_detail_label_max_pace));
        }
//        }
        SportRecordDataAdapter sportRecordDataAdapter = new SportRecordDataAdapter(valueList, nameList);
        speedList.setAdapter(sportRecordDataAdapter);
    }

    private void setAltitudeList(View view) {
        RecyclerView altitudeList = view.findViewById(R.id.altitudeList);
        altitudeList.setLayoutManager(new GridLayoutManager(getContext(), 3));
        List<String> valueList = new ArrayList<>();
        List<String> nameList = new ArrayList<>();
        WorkoutExtraInfo workoutExtraInfo = workout.getWorkoutExtraInfo();
        if (workoutExtraInfo.getMinAltitude() == workoutExtraInfo.getMaxAltitude()) {
            return;
        }
        valueList.add(decimalFormat2.format(workoutExtraInfo.getMaxAltitude()) + getString(R.string.unit_m));
        nameList.add(getString(R.string.workout_chart_label_max_altitude_pro));
        valueList.add(decimalFormat2.format(workoutExtraInfo.getMinAltitude()) + getString(R.string.unit_m));
        nameList.add(getString(R.string.sport_tag_min_altitude));
        valueList.add(decimalFormat2.format(workoutExtraInfo.getAvgAltitude()) + getString(R.string.unit_m));
        nameList.add(getString(R.string.sport_tag_avg_altitude));
        valueList.add((int) workout.getElevationGain() + getString(R.string.unit_m));
        nameList.add(getString(R.string.workout_share_pro_label_elevation_gain));
        valueList.add((int) workout.getElevationLoss() + getString(R.string.unit_m));
        nameList.add(getString(R.string.sport_tag_elevation_lost));
        valueList.add(workout.getMinGrade() + "% ~ " + workout.getMaxGrade() + "%");
        nameList.add(getString(R.string.workout_detail_label_grade));
        SportRecordDataAdapter sportRecordDataAdapter = new SportRecordDataAdapter(valueList, nameList);
        altitudeList.setAdapter(sportRecordDataAdapter);
    }

    private void setPowerList(View view) {
        RecyclerView powerList = view.findViewById(R.id.powerList);
        powerList.setLayoutManager(new GridLayoutManager(getContext(), 3));
        List<String> valueList = new ArrayList<>();
        List<String> nameList = new ArrayList<>();
        WorkoutExtraInfo workoutExtraInfo = workout.getWorkoutExtraInfo();
        if (workoutExtraInfo.getMaxPower() <= Utils.DOUBLE_EPSILON) {
            return;
        }
        valueList.add((int) workoutExtraInfo.getAvgPower() + getString(R.string.unit_power));
        nameList.add(getString(R.string.workout_chart_label_avg_power_pro));
        valueList.add((int) workoutExtraInfo.getMaxPower() + getString(R.string.unit_power));
        nameList.add(getString(R.string.workout_chart_label_max_power_pro));
        valueList.add((int) workout.getPowerFTP() + getString(R.string.unit_power));
        nameList.add(getString(R.string.workout_chart_label_power_ftp));
        valueList.add(decimalFormat2.format(workout.getPowerNP()) + getString(R.string.unit_power));
        nameList.add(getString(R.string.workout_chart_power_standardization));
        valueList.add(decimalFormat4.format(workout.getPowerIF()));
        nameList.add(getString(R.string.workout_chart_power_intensity_factor));
        valueList.add(decimalFormat.format(workout.getPowerVI()));
        nameList.add(getString(R.string.workout_chart_label_VI_power));
        valueList.add(decimalFormat2.format(workout.getPowerTSS()));
        nameList.add(getString(R.string.workout_chart_label_TSS_power));
        SportRecordDataAdapter sportRecordDataAdapter = new SportRecordDataAdapter(valueList, nameList);
        powerList.setAdapter(sportRecordDataAdapter);
    }

    private void setHeartRateList(View view) {
        RecyclerView heartRateList = view.findViewById(R.id.heartRateList);
        heartRateList.setLayoutManager(new GridLayoutManager(getContext(), 3));
        List<String> valueList = new ArrayList<>();
        List<String> nameList = new ArrayList<>();
        if (workout.getMaxHeartrate() == 0) {
            return;
        }
        valueList.add(workout.getAvgHeartrate() + getString(R.string.unit_bpm));
        nameList.add(getString(R.string.workout_detail_label_avg_heartrate));
        valueList.add(workout.getMaxHeartrate() + getString(R.string.unit_bpm));
        nameList.add(getString(R.string.workout_detail_label_max_heartrate));
        SportRecordDataAdapter sportRecordDataAdapter = new SportRecordDataAdapter(valueList, nameList);
        heartRateList.setAdapter(sportRecordDataAdapter);
    }

    private void setCadenceList(View view) {
        RecyclerView cadenceList = view.findViewById(R.id.cadenceList);
        cadenceList.setLayoutManager(new GridLayoutManager(getContext(), 3));
        List<String> valueList = new ArrayList<>();
        List<String> nameList = new ArrayList<>();
        if (workout.getMaxCadence() == 0) {
            return;
        }
        valueList.add(workout.getAvgCadence() + getString(R.string.unit_rpm));
        nameList.add(getString(R.string.workout_detail_label_avg_cadence));
        valueList.add(workout.getMaxCadence() + getString(R.string.unit_rpm));
        nameList.add(getString(R.string.workout_detail_label_max_cadence));
        SportRecordDataAdapter sportRecordDataAdapter = new SportRecordDataAdapter(valueList, nameList);
        cadenceList.setAdapter(sportRecordDataAdapter);
    }

    private void setGeneralList(View view) {
        RecyclerView generalList = view.findViewById(R.id.generalList);
        generalList.setLayoutManager(new GridLayoutManager(getContext(), 3));
        List<String> valueList = new ArrayList<>();
        List<String> nameList = new ArrayList<>();
        valueList.add(Math.round(workout.getCalorie() / 1000) + "kcal");
        nameList.add(getString(R.string.workout_detail_label_calorie));
        if (workout.getServerId() > 0) {
            valueList.add(Separators.POUND + workout.getServerId());
        } else {
            valueList.add(getString(R.string.workout_label_not_upload));
        }
        nameList.add(getString(R.string.workout_detail_label_id));
        SportRecordDataAdapter sportRecordDataAdapter = new SportRecordDataAdapter(valueList, nameList);
        generalList.setAdapter(sportRecordDataAdapter);
    }
}